<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>防抖节流</title>
</head>

<body>
    <script>
        function debounce(fn, wait) {
            let timer = null

            return function () {
                let context = this, args = arguments

                if (timer) {
                    clearTimeout(timer)
                    timer = null
                }

                timer = setTimeout(() => { fn.apply(context, args) }, wait)
            }
        }


        function throttle(fn, delay) {
            var preTime = Date.now()

            return function () {
                let context = this, args = arguments, nowTime = Date.now()

                if (nowTime - preTime > delay) {
                    preTime = Date.now()
                    return fn.apply(context, args)
                }
            }
        }

    </script>
</body>

</html>